home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / hard / misc / CheckMem.lha / checkmem.c < prev    next >
C/C++ Source or Header  |  1994-12-11  |  4KB  |  168 lines

  1. /*
  2.  * Program: CheckMem
  3.  * Author: Osma Ahvenlampi
  4.  * Description: Check memory
  5.  *
  6.  */
  7.  
  8. #include <exec/memory.h>
  9. #include <exec/types.h>
  10. #include <exec/nodes.h>
  11. #include <exec/lists.h>
  12. #include <exec/execbase.h>
  13. #include <dos/rdargs.h>
  14.  
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17.  
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21.  
  22. #define TEMPLATE "FROM,TO,TIMES/N/K,FORCE/S,QUIET/S"
  23. #define ARG_FROM 0
  24. #define ARG_TO 1
  25. #define ARG_TIMES 2
  26. #define ARG_FORCE 3
  27. #define ARG_QUIET 4
  28. #define ARGNUM 5
  29.  
  30. #define VERSION "1.1"
  31.  
  32. extern struct ExecBase *SysBase;
  33.  
  34. LONG ArgArray[ARGNUM];
  35. BOOL quiet;
  36.  
  37. const char VersionID[] = "$VER: CheckMem " VERSION "(11.12.94)";
  38.  
  39. ULONG TestVals[] = { 0x00000000, 0xffffffff, 0xaaaaaaaa, 0x55555555 };
  40.  
  41. ULONG __asm CheckMemBlock( register __a0 UBYTE * );
  42.  
  43. void PrintList( struct MemHeader * );
  44.  
  45. int CheckMemRegion( UBYTE *, UBYTE * );
  46.  
  47. void __regargs __chkabort(void) {}
  48.  
  49. void 
  50. main(char *arg, int argv)
  51. {
  52.     struct RDArgs *RDArgs;
  53.     UBYTE *low, *high;
  54.     struct MemHeader *l;
  55.     int error = 0, i;
  56.     time_t t;
  57.  
  58.     SetSignal( 0L, SIGBREAKF_CTRL_C );
  59.  
  60.     if (RDArgs = ReadArgs(TEMPLATE, ArgArray, NULL))
  61.     {
  62.         LONG oldpri;
  63.  
  64.         (void)time(&t);
  65.         
  66.         printf("CheckMem " VERSION " by Osma Ahvenlampi\n"
  67.             "Memory check started on %s\n", ctime(&t));
  68.         
  69.         i = ArgArray[ARG_TIMES] ? *(int *)ArgArray[ARG_TIMES] : 1;
  70.         quiet = (BOOL)ArgArray[ARG_QUIET];
  71.  
  72.         if ((oldpri = SetTaskPri(FindTask(NULL),-1)) < -1)
  73.             SetTaskPri(FindTask(NULL),oldpri);
  74.         
  75.         if (ArgArray[ARG_FROM] && ArgArray[ARG_TO])
  76.         {
  77.             BOOL within = FALSE;
  78.             low = (UBYTE *) strtoul((char *)ArgArray[ARG_FROM], NULL, 16);
  79.             high = (UBYTE *) strtoul((char *)ArgArray[ARG_TO], NULL, 16);
  80.  
  81.             for ( l = (struct MemHeader *)SysBase->MemList.lh_Head ;
  82.                      l->mh_Node.ln_Succ ; l=(struct MemHeader *)l->mh_Node.ln_Succ )
  83.             {
  84.                 if (low >= (UBYTE *)l && high <= (UBYTE *)l->mh_Upper)
  85.                     within = TRUE;
  86.             }
  87.             
  88.             if (!within)
  89.             {
  90.                 if (!ArgArray[ARG_FORCE])
  91.                 {
  92.                     printf("Addresses outside Exec MemList. Use FORCE keyword if you really mean it.\n");
  93.                     exit(20);
  94.                 }
  95.                 if (FindTask("« Enforcer »"))
  96.                 {
  97.                     printf("Can't check addresses outside Exec MemList with Enforcer on.\n");
  98.                     exit(20);
  99.                 }
  100.             }
  101.             while ( i-- )
  102.             {
  103.                 if (!quiet)
  104.                     printf("Checking memory from 0x%x to 0x%x...\n", low, high);
  105.                 error |= CheckMemRegion( low, high );
  106.                 if (SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C)
  107.                     goto ctrlc;
  108.             }
  109.         }
  110.         else
  111.         {
  112.             while ( i-- )
  113.             {
  114.                 for ( l = (struct MemHeader *)SysBase->MemList.lh_Head ;
  115.                      l->mh_Node.ln_Succ ; l=(struct MemHeader *)l->mh_Node.ln_Succ )
  116.                 {
  117.                     if (!quiet)
  118.                         printf("Checking %s from 0x%x to 0x%x...\n", l->mh_Node.ln_Name, l, (UBYTE *)l->mh_Upper-1);
  119.                     error |= CheckMemRegion( (UBYTE *)l, (UBYTE *)l->mh_Upper-1 );
  120.                     if (SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C)
  121.                         goto ctrlc;
  122.                 }
  123.             }
  124.         }
  125. ctrlc:
  126.         FreeArgs(RDArgs);
  127.     }
  128.     else
  129.         PrintFault(IoErr(),"CheckMem");
  130.     if (error)
  131.         printf("Errors detected.\n");
  132.     exit(error ? 10 : 0);
  133. }
  134.  
  135. int
  136. CheckMemRegion(UBYTE *low, UBYTE *high)
  137. {
  138.     int error = 0;
  139.     ULONG bits;
  140.     while (low + 31 < high)
  141.     {
  142.         if (!((ULONG) low % 0x40000))
  143.         {
  144.             if (SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C)
  145.             {
  146.                 PrintFault(ERROR_BREAK,NULL);
  147.                 return error;
  148.             }
  149.             if (!quiet)
  150.             {
  151.                 putchar('.');
  152.                 fflush(stdout);
  153.             }
  154.         }
  155.         
  156.         if (bits = CheckMemBlock(low)) /* check 32 bytes.. disables interrupts! */
  157.         {
  158.             error = 1;
  159.             printf("\nProblem between 0x%x .. 0x%x, bitpattern 0x%8x\n", low, low + 31, bits);
  160.             fflush(stdout);
  161.         }
  162.         low += 32;
  163.     }
  164.     if (!quiet)
  165.         putchar('\n');
  166.     return error;
  167. }
  168.